<template>
  <span :title="isFullscreen ? '退出全屏' : '进入全屏'">
    <svg-icon :icon-class="isFullscreen ? 'quit-full-screen' : 'full-screen'" @click="click" style="font-size: 22px"/>
  </span>
</template>

<script>
import screenfull from 'screenfull'

export default {
  name: 'VabFullScreenBar',
  data() {
    return {
      isFullscreen: false
    }
  },
  mounted() {
    this.init()
  },
  beforeDestroy() {
    this.destroy()
  },
  methods: {
    click() {
      if (!screenfull.isEnabled) {
        this.$baseMessage('开启全屏失败', 'error')
        return false
      }
      screenfull.toggle()
      // 延时刷新是为了解决全屏之后树形结构无法界面变大的问题
      setTimeout(() => {
        this.$emit('refresh')
      }, 1000)
    },
    change() {
      this.isFullscreen = screenfull.isFullscreen
    },
    init() {
      if (screenfull.isEnabled) {
        screenfull.on('change', this.change)
      }
    },
    destroy() {
      if (screenfull.isEnabled) {
        screenfull.off('change', this.change)
      }
    }
  }
}
</script>
<style scoped>
.svg-icon {
  color: #FFFFFF;
}
</style>
